﻿// ———————————————————————–
// <copyright file="Program.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// Program.cs - Console Application to load XML data into DynamoDB
// Project: EDXLSharp_AWSRouter- DynamoDBLoader
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010
// Author:      Don McGarry The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using ICNETSQLObjectLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace DynamoDBLoader
{
  /// <summary>
  /// Main entry point for the console application
  /// </summary>
  public class Program
  {
    /// <summary>
    /// Amazon DynamoDB configuration object 
    /// </summary>
    private static AmazonDynamoDBConfig amazonDynamoConfig;

    /// <summary>
    /// Amazon DynamoDB client
    /// </summary>
    private static AmazonDynamoDBClient amazonDynamoClient;

    /// <summary>
    /// Main entry point method for the console application
    /// </summary>
    /// <param name="args">Command line arguments</param>
    public static void Main(string[] args)
    {
      amazonDynamoConfig = new AmazonDynamoDBConfig();
      amazonDynamoConfig.ServiceURL = "http://dynamodb.us-east-1.amazonaws.com";
      amazonDynamoClient = new AmazonDynamoDBClient(amazonDynamoConfig);
      List<DBValueList> valueLists = new List<DBValueList>();
      XmlSerializer serial = new XmlSerializer(typeof(List<DBValueList>));
      StreamReader sr = new StreamReader("ValueLists.xml");
      valueLists = (List<DBValueList>)serial.Deserialize(sr);
      sr.Close();
      foreach (DBValueList vl in valueLists)
      {
        PutItemRequest dynamoreq = new PutItemRequest();
        Dictionary<string, AttributeValue> dynamoitem = new Dictionary<string, AttributeValue>();
        AttributeValue dynamovalue = new AttributeValue();
        dynamovalue.S = vl.ValueListURI;
        dynamoitem.Add("ValueListURI", dynamovalue);
        dynamovalue = new AttributeValue();
        dynamovalue.S = vl.Value;
        dynamoitem.Add("Value", dynamovalue);
        if (!String.IsNullOrEmpty(vl.CommandStructure))
        {
          dynamovalue = new AttributeValue();
          dynamovalue.S = vl.CommandStructure;
          dynamoitem.Add("CommandStructure", dynamovalue);
        }

        if (!String.IsNullOrEmpty(vl.Description))
        {
          dynamovalue = new AttributeValue();
          dynamovalue.S = vl.Description;
          dynamoitem.Add("Description", dynamovalue);
        }
        dynamoreq.TableName = "ValueLists";
        dynamoreq.Item = dynamoitem;
        PutItemResponse dynamoresp = amazonDynamoClient.PutItem(dynamoreq);
        if (dynamoresp.HttpStatusCode == System.Net.HttpStatusCode.OK)
        {
          Console.WriteLine("Message Added to Dynamo");
        }
        else
        {
          Console.WriteLine("Dynamo Error" + dynamoresp.HttpStatusCode.ToString());
        }
      }

      Console.WriteLine("ValueLists Loaded");
      List<DBRule> rules = new List<DBRule>();
      serial = new XmlSerializer(typeof(List<DBRule>));
      sr = new StreamReader("Rules.xml");
      rules = (List<DBRule>)serial.Deserialize(sr);
      sr.Close();
      foreach (DBRule r in rules)
      {
        PutItemRequest dynamoreq = new PutItemRequest();
        Dictionary<string, AttributeValue> dynamoitem = new Dictionary<string, AttributeValue>();
        AttributeValue dynamovalue = new AttributeValue();
        dynamovalue.S = r.FromListURI + r.FromListValue;
        dynamoitem.Add("FromListURI", dynamovalue);
        dynamovalue = new AttributeValue();
        dynamovalue.S = r.ToListURI + r.ToListValue;
        dynamoitem.Add("ToListURI", dynamovalue);
        dynamovalue = new AttributeValue();
        dynamovalue.S = r.RuleType;
        dynamoitem.Add("RuleType", dynamovalue);
        dynamoreq.Item = dynamoitem;
        dynamoreq.TableName = "Rules";
        PutItemResponse dynamoresp = amazonDynamoClient.PutItem(dynamoreq);
        if (dynamoresp.HttpStatusCode == System.Net.HttpStatusCode.OK)
        {
          Console.WriteLine("Message Added to Dynamo");
        }
        else
        {
          Console.WriteLine("Dynamo Error" + dynamoresp.HttpStatusCode.ToString());
        }
      }

      Console.WriteLine("Rules Loaded");
      Console.ReadKey();
    }
  }
}
